<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="listQuery.name" size="small" placeholder="名称" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="handleFilter">筛选</el-button>
        <el-button size="small" @click="resetFilter">重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toDepartmentNew"
        >添加科室</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="位置" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.place }}
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <el-button
              size="small"
              plain
            >操作 <i class="el-icon-arrow-down el-icon--right" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="toDepartmentEdit(scope.row.id)"
              >编辑</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="setTop(scope.row.id)"
              >置顶</el-dropdown-item>
              <el-dropdown-item
                @click.native="moveUp(scope.row.id)"
              >上移</el-dropdown-item>
              <el-dropdown-item
                @click.native="moveDown(scope.row.id)"
              >下移</el-dropdown-item>
              <el-dropdown-item
                @click.native="setBottom(scope.row.id)"
              >置底</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="removeDepartment(scope.row.id)"
              >
                <span class="text-danger">删除</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getDepartments
} from '@/api/baseData'
import {
  removeDepartment,
  departmentSetTop,
  departmentSetBottom,
  departmentMoveUp,
  departmentMoveDown
} from '@/api/departments'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'DepartmentSettingList',
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        name: formatQuery(this.$route.query.name, false)
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getDepartments(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      // this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.handleFilter()
    },
    toDepartmentNew() {
      this.$router.push('/setting/departments/new')
    },
    toDepartmentEdit(id) {
      this.$router.push(`/setting/departments/edit/${id}`)
    },
    removeDepartment(id) {
      this.$confirm('此操作将永久删除该科室, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeDepartment({ id }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    setTop(id) {
      departmentSetTop({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    setBottom(id) {
      departmentSetBottom({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    moveUp(id) {
      departmentMoveUp({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    moveDown(id) {
      departmentMoveDown({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
